package com.tanhua.dubbo.api.db;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tanhua.domain.db.UserInfo;
import com.tanhua.dubbo.mapper.UserInfoMapper;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;

@Service
public class UserInfoApiImpl implements UserInfoApi {

    @Autowired
    private UserInfoMapper userInfoMapper;

    @Override
    public void save(UserInfo userInfo) {
        userInfoMapper.insert(userInfo);
    }

    @Override
    public void update(UserInfo userInfo) {
        userInfoMapper.updateById(userInfo);
    }

    @Override
    public UserInfo findById(Long id) {
        return userInfoMapper.selectById(id);
    }

    @Override
    public IPage<UserInfo> queryUsers(Integer page, Integer pagesize, Long id, String nickname, String city) {
        QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<>();
//        Long id, String nickname, String city
        if(id!=null){
            queryWrapper.lambda().eq(UserInfo::getId,id);
        }
        if(StringUtils.isNotBlank(nickname)){
            queryWrapper.lambda().eq(UserInfo::getNickname,nickname);
        }
        if(StringUtils.isNotBlank(city)){
            queryWrapper.lambda().like(UserInfo::getCity,city);
        }

        Page page1 = new Page(page,pagesize);
        IPage iPage = userInfoMapper.selectPage(page1, queryWrapper);
        return iPage;
    }

    @Override
    public List<UserInfo> findAll() {
        QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<>();
        //queryWrapper.exists()
        List<UserInfo> userInfoList = userInfoMapper.selectList(queryWrapper);
        return userInfoList;
    }
}
